import 'package:flutter/material.dart';

enum TransitionType {
  inFromLeft,
  inFromRight,
  inFromTop,
  inFromBottom,
  scale,
  fade,
  rotation,
  size,
}

Future<T?> showAnimationDialog<T>({
  required BuildContext context,
  bool barrierDismissible = true,
  @Deprecated('') required WidgetBuilder builder,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  TransitionType? transitionType,
}) {
  assert(debugCheckHasMaterialLocalizations(context));

  // ignore: unnecessary_nullable_for_final_variable_declarations
  final ThemeData? theme = Theme.of(context);
  return showGeneralDialog<T?>(
    context: context,
    pageBuilder: (BuildContext buildContext, Animation<double> animation,
        Animation<double> secondaryAnimation) {
      return Builder(builder: (BuildContext context) {
        return theme != null
            ? Theme(data: theme, child: builder(context))
            : builder(context);
      });
    },
    barrierDismissible: barrierDismissible,
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
    barrierColor: Colors.black54,
    transitionDuration: const Duration(milliseconds: 300),
    transitionBuilder: (context, animation1, animation2, child) {
      return _buildDialogTransitions(
          context, animation1, animation2, child, transitionType!);
    },
    useRootNavigator: useRootNavigator,
    routeSettings: routeSettings,
  );
}

Widget _buildDialogTransitions(
    BuildContext context,
    Animation<double> animaton1,
    Animation<double> secondaryAnimation,
    Widget child,
    TransitionType type) {
  if (type == TransitionType.fade) {
    // Fade effect
    return FadeTransition(
      opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
        parent: animaton1,
        curve: Curves.fastOutSlowIn,
      )),
      child: child,
    );
  } else if (type == TransitionType.scale) {
    return ScaleTransition(
      scale: Tween(begin: 0.0, end: 1.0).animate(
          CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  } else if (type == TransitionType.rotation) {
    // Rotation and scale animation
    return RotationTransition(
      turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
        parent: animaton1,
        curve: Curves.fastOutSlowIn,
      )),
      child: ScaleTransition(
        scale: Tween(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
        child: child,
      ),
    );
  } else if (type == TransitionType.inFromLeft) {
    // Slide in from left animation
    return SlideTransition(
      position: Tween<Offset>(
              begin: const Offset(-1.0, 0.0), end: const Offset(0.0, 0.0))
          .animate(
              CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  } else if (type == TransitionType.inFromRight) {
    // Slide in from right animation
    return SlideTransition(
      position: Tween<Offset>(
              begin: const Offset(1.0, 0.0), end: const Offset(0.0, 0.0))
          .animate(
              CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  } else if (type == TransitionType.inFromTop) {
    // Slide in from top animation
    return SlideTransition(
      position: Tween<Offset>(
              begin: const Offset(0.0, -1.0), end: const Offset(0.0, 0.0))
          .animate(
              CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  } else if (type == TransitionType.inFromBottom) {
    // Slide in from bottom animation
    return SlideTransition(
      position: Tween<Offset>(
              begin: const Offset(0.0, 1.0), end: const Offset(0.0, 0.0))
          .animate(
              CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  } else if (type == TransitionType.size) {
    // Size animation
    return SizeTransition(
      sizeFactor: Tween<double>(begin: 0.1, end: 1.0)
          .animate(CurvedAnimation(parent: animaton1, curve: Curves.linear)),
      child: child,
    );
  } else {
    return child;
  }
}


/*

showAnimationDialog(
  context: context,
  builder: (context) => AlertDialog(
    title: Text('Hello'),
    content: Text('This is an animated dialog!'),
  ),
  transitionType: TransitionType.fade, // 选择动画类型
);


*/

/*
  showAnimationDialog(
              context: context,
              builder: (context) => _dialogContent(),
              transitionType: TransitionType.inFromRight, // 选择动画类型
              barrierDismissible: true,
            );

  Widget _dialogContent() {
    return AlertDialog(
      title: Text('Hello'),
      content: Text('This is an animated dialog'),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: Text('Close'),
        ),
      ],
    );
  }
*/